clc, clear all,close all; % clearing matlab memory and command window

%% 3D Permeability/Porosity Upscaling Single Fase Flow
% (by now this work fine with the Tenth SPE Comparative Project Models

% one thing that inst working is using the well refinament tratament with
% final grids not exactly divisible from the original)

% by jose augusto f. ramos (zeaugustofr@gmail.com)                        %
%                                                                         %
% last updade date: 22/08/2011    

profile on %profile gives the execution time for each function

%% input - reading case data

%% input_data is a matlab function who gets in this order from input.txt:

% IMEX .exe adress (with that u can run automatically the new .dat)
% IMEX file (.dat) adress (the one who will be upscaled)
% output folder adress (for obvious things)
% nsize -> upscaling new grid size (this is an important one)
% refino -> well block treatment (1 = keep the refinement)
%     (for more information see topic 4.6 on Filho, D. S. M. 2009)
% transmi -> transmissibility upscaling (only for flowbased method)
%     (for more information see topic 5 on Durlofsky, L. J. 2003)
% method -> choice of method for the transfer of scale
%     (on this program: pythagorean means and flow based method)

[imex,arquivo,outfolder,nsize,refino,transmi,method] = input_data;

%% input_imex is a matlab function who gets from imex original .dat file:

% i, j, k; original grid size
% DI ,DJ ,DK; direction block dimensions
% PHI; An array of geostatistically distributed values is used
%for porosity;  
% KI, KJ, KK; An array of geostatistically distributed values is
%used for permeability;
% wells; well indentification (location)

[i,j,k,DI,DJ,DK,PHI,KI,KJ,KK,wells] = input_imex(arquivo);

%% solver - upscaling porosity/permeability

%% solver_preparation is a matlab function who ...

% new grid size - ni,nj,nk - from nsize vector 
% number of cells in each direction within the blocks - ncells
% (this one deserves a parenthesis because we have programmed a routine
% that allows final grids not exactly divisible from the original. With
% that the number of cells within the blocks r not constant and requires a
% special treatment that was solve using the matlab structure 'cells'.)
% and this function also takes but no less important 'the direction block
% dimensions' nDI,nDJ,nDK (the new cells 'dx') (this also gets a resave
% because if we r using the routine mentioned above the block dimensions
% also r not constant! for more information see Ramos, J.A.F 2011)

[ni,nj,nk,cells_i,cells_j,cells_k,nDI,nDJ,nDK,np] = ...
    solver_preparation(i,j,k,DI,DJ,DK,nsize);

%% solver is a matlab function who do

% porosity upscaling - arithmetic mean;
% permeability upscaling - pythagorean means and flow based method:
% and create  the new IMEX .dat file with imex_export function (if you
% want u can run it already to but now on test phase i disable this)
% this function uses all the things we have here so i wont talk about much

solver(arquivo,outfolder,method,refino,transmi,i,j,k,ni,nj,nk,np,cells_i,cells_j,cells_k,...
            nDI,nDJ,nDK,PHI,KI,KJ,KK,DI,DJ,DK,wells)
        
%% exiting the program

msgbox('END OF SIMULATION'); %YEAAAAAAAH!!
profile viewer
profile off